﻿using AutoMapper;
using DailyApp.API.ApiResponses;
using DailyApp.API.DTOs;
using DailyApp.API.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DailyApp.API.Controllers
{
    /// <summary>
    /// 备忘录接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemoController : ControllerBase
    {
        private readonly STJCAMContext db;
        private readonly IMapper mapper;

        public MemoController(STJCAMContext _db, IMapper _mapper)
        {
            db = _db;
            mapper = _mapper;
        }

        /// <summary>
        /// 统计备忘录总数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StatMemo()
        {
            ApiResponse res = new ApiResponse();
            try
            {
                int count=db.EfmShareFolderPermissions.Where(t => t.Permissions == "Memo").ToList().Count;
                res.ResultCode = 1;
                res.Msg = "统计备忘录成功";
                res.ResultData = count;
            }
            catch (Exception ex)
            {
                res.ResultCode = -99;
                res.Msg = ex.Message;
            }
            return Ok(res);
        }

        /// <summary>
        /// 添加备忘录
        /// </summary>
        /// <param name="memoDTO"></param>
        /// <returns>1：添加成功  -99：异常</returns>
        [HttpPost]
        public IActionResult AddMemo(MemoDTO memoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                //DTO->EfmShareFolderPermissions
                EfmShareFolderPermission efmShareFolderPermission = mapper.Map<EfmShareFolderPermission>(memoDTO);
                db.EfmShareFolderPermissions.Add(efmShareFolderPermission);
                int result = db.SaveChanges();
                if (result == 1)
                {
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "添加备忘录成功";
                }
                else
                {
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "添加备忘录失败";
                }
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 查询备忘录
        /// </summary>
        /// <param name="badge"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult QueryMemo(string? badge)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var query = from A in db.EfmShareFolderPermissions
                            where A.Permissions == "Memo"
                            select new MemoDTO
                            {
                                Badge = A.Badge,
                                Name = A.Name,
                                Status=A.Status,
                                FilePath=A.FilePath,
                                Permissions=A.Permissions,
                                CreateBy = A.CreateBy,
                                CreateTime = A.CreateTime,
                                Rowguid=A.Rowguid,
                            };
                if(!string.IsNullOrEmpty(badge))
                {
                    query=query.Where(x => x.Badge.Contains(badge));
                }
                apiResponse.ResultCode = 1;
                apiResponse.Msg = "查询成功";
                apiResponse.ResultData = query;
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 修改备忘录
        /// </summary>
        /// <param name="memoDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult EditMemo(MemoDTO memoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var dbInfo = db.EfmShareFolderPermissions.Where(e => e.Rowguid == memoDTO.Rowguid).FirstOrDefault();
                if (dbInfo != null)
                {
                    var newdbInfo = dbInfo;
                    db.EfmShareFolderPermissions.Remove(dbInfo);
                    int result = db.SaveChanges();
                    newdbInfo.Status = memoDTO.Status.ToString();
                    newdbInfo.Badge = memoDTO.Badge.ToString();
                    newdbInfo.Name = memoDTO.Name.ToString();
                    newdbInfo.CreateTime = DateTime.Now;
                    db.EfmShareFolderPermissions.Add(newdbInfo);
                    result = db.SaveChanges();
                    if (result == 1)
                    {
                        apiResponse.ResultCode = 1;
                        apiResponse.Msg = "修改成功";
                    }
                    else
                    {
                        apiResponse.ResultCode = -1;
                        apiResponse.Msg = "修改失败";
                    }
                }
                else
                {
                    apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
                }
            }
            catch (Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }

        /// <summary>
        /// 删除备忘录
        /// </summary>
        /// <param name="rowguid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMemo(string rowguid)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                var dbInfo=db.EfmShareFolderPermissions.FirstOrDefault(x=>x.Rowguid.ToString()==rowguid);
                if (dbInfo == null)
                {
                    apiResponse.ResultCode = -2;
                    apiResponse.Msg = "为空";
                    return Ok(apiResponse);
                }
                db.EfmShareFolderPermissions.Remove(dbInfo);
                int res = db.SaveChanges();
                if (res == 1)
                {
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "删除成功";
                }
                else
                {
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "删除失败";
                }
            }
            catch(Exception ex)
            {
                apiResponse.ResultCode = -99; apiResponse.Msg = "服务器繁忙...";
            }
            return Ok(apiResponse);
        }
    }
}
